package service;

import entity.Member;
import entity.MemberAddress;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

import java.util.List;

/**
 * @program: Mybatis201759225309
 * @description: MemberDao 实现
 * @author: Xiaozhuoer
 * @create: 2021-04-12 11:21
 **/

public class MemberImpl implements dao.MemberDao {
    @Override
    public int addMember(Member member) {
        SqlSession session = MybatisUtil.getSqlSession();
        session.insert("addMember",member);
        session.commit();
        session.close();
        System.out.println("insert finish");
        return 1;
    }

    @Override
    public int addThreeMember() {
        return 0;
    }

    @Override
    public int delMemberFromName(String username) {
        Member member = new Member();
        member.setUser_name(username);
        SqlSession session = MybatisUtil.getSqlSession();
        session.delete("delMemberFromName",member);
        session.commit();
        session.close();
        System.out.println("delete finish");
        return 1;
    }

    @Override
    public List<Member> selectMemberAll() {
        SqlSession session = MybatisUtil.getSqlSession();
        Member member= new Member();
        List<Member> memberList= session.selectList("selectMemberAll");
        session.commit();
        session.close();
        System.out.println("selectMemberAll finish");
        return memberList;
    }

    @Override
    public List<Member> selectMemberWhereIf(int id, String username, String mobile) {
        SqlSession session = MybatisUtil.getSqlSession();
        Member member= new Member();
        member.setId(id);
        member.setUser_name(username);
        member.setMobile(mobile);
        List<Member> memberList= session.selectList("selectMemberWhereIfIdAndUserNameAndMobile",member);
        session.commit();
        session.close();
        System.out.println("selectMemberWhereIf finish");
        return memberList;
    }
    @Override
    public List<Member> selectMemberTrimChoose(int id, String username, String mobile) {
        SqlSession session = MybatisUtil.getSqlSession();
        Member member= new Member();
        member.setId(id);
        member.setUser_name(username);
        member.setMobile(mobile);
        List<Member> memberList= session.selectList("selectMemberTrimChooseIdAndUserNameAndMobile",member);
        session.commit();
        session.close();
        System.out.println("selectMemberTrimChoose finish");
        return memberList;
    }

    @Override
    public List<Member> selectMemberAndMemberAddressByMemberIdOneToOne(int id) {
        SqlSession session = MybatisUtil.getSqlSession();
        Member member= new Member();
        member.setId(id);
        List<Member> memberList= session.selectList("selectMemberAndMemberAddressByMemberIdOneToOne",member);
        session.commit();
        session.close();
        System.out.println("selectMemberAndMemberAddressByMemberIdOneToOne finish");
        return memberList;
    }

    @Override
    public List<Member> selectMemberAndMemberAddressByMemberIdOneToMore(int id) {
        SqlSession session = MybatisUtil.getSqlSession();
        Member member= new Member();
        member.setId(id);
        List<Member> memberList= session.selectList("selectMemberAndMemberAddressByMemberIdOneToMore",member);
        session.commit();
        session.close();
        System.out.println("selectMemberAndMemberAddressByMemberIdOneToMore finish");
        return memberList;
    }

}
